package com.miplus.ccrm.workflow.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.EnumRCode;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.ServiceException;
import com.miplus.ccrm.workflow.model.common.DeleteRequest;
import com.miplus.ccrm.workflow.model.dto.knowledge.KnowledgeQuestionCreateDTO;
import com.miplus.ccrm.workflow.model.dto.knowledge.KnowledgeQuestionQueryDTO;
import com.miplus.ccrm.workflow.model.dto.knowledge.KnowledgeQuestionUpdateDTO;
import com.miplus.ccrm.workflow.model.po.WorkflowKnowledgeBase;
import com.miplus.ccrm.workflow.model.po.WorkflowKnowledgeQuestion;
import com.miplus.ccrm.workflow.model.vo.KnowledgeQuestionVO;
import com.miplus.ccrm.workflow.service.WorkflowKnowledgeBaseService;
import com.miplus.ccrm.workflow.service.WorkflowKnowledgeQuestionService;
import com.miplus.ccrm.workflow.mapper.WorkflowKnowledgeQuestionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author feixiang
 * @description 针对表【workflow_knowledge_question(知识库问题表)】的数据库操作Service实现
 * @createDate 2025-11-04 20:48:14
 */
@Service
public class WorkflowKnowledgeQuestionServiceImpl extends MPJBaseServiceImpl<WorkflowKnowledgeQuestionMapper, WorkflowKnowledgeQuestion>
        implements WorkflowKnowledgeQuestionService {

    @Resource
    private WorkflowKnowledgeBaseService knowledgeBaseService;

    @Override
    public boolean deleteById(Long id) {
        return false;
    }

    @Override
    public CmdResVo cereateKnowledgeQuestion(KnowledgeQuestionCreateDTO questionDTO) {
        if (questionDTO == null) {
            throw new ServiceException(EnumRCode.ArgFormatErr.value, EnumRCode.ArgFormatErr.name);
        }
        //判断问题对应知识库是否存在
        WorkflowKnowledgeBase kb = knowledgeBaseService.getById(questionDTO.getKbId());
        if (kb == null) {
            throw new ServiceException(EnumRCode.DataNotExist.value, "知识库不存在");
        }
        WorkflowKnowledgeQuestion question = new WorkflowKnowledgeQuestion();
        BeanUtils.copyProperties(questionDTO, question);
        boolean result = this.save(question);
        return new CmdResVo(1, question.getPid().toString());
    }

    @Override
    public CmdResVo updateKnowledgeQuestion(KnowledgeQuestionUpdateDTO questionDTO) {
        if (questionDTO == null) {
            throw new ServiceException(EnumRCode.ArgFormatErr.value, EnumRCode.ArgFormatErr.name);
        }
        //判断知识库问题表，知识库对应的下的问题id和知识库id，是否存在记录
        WorkflowKnowledgeQuestion one = this.lambdaQuery()
                .eq(WorkflowKnowledgeQuestion::getPid, questionDTO.getQuestionId())
                .eq(WorkflowKnowledgeQuestion::getKbId, questionDTO.getKbId())
                .one();
        if (one == null) {
            throw new ServiceException(EnumRCode.DataNotExist.value, "该知识库下问题不存在");
        }

        BeanUtils.copyProperties(questionDTO, one, "kbId", "questionId");
        boolean result = this.updateById(one);
        return new CmdResVo(1, one.getPid().toString());
    }

    @Override
    public CmdResVo deleteKnowledgeQuestion(DeleteRequest deleteRequest) {

        if (deleteRequest == null) {
            throw new ServiceException(EnumRCode.ArgFormatErr.value, EnumRCode.ArgFormatErr.name);
        }
        //判断知识库问题表，知识库对应的下的问题id和知识库id，是否存在记录
        WorkflowKnowledgeQuestion one = this.lambdaQuery()
                .eq(WorkflowKnowledgeQuestion::getPid, deleteRequest.getId())
                .one();
        if (one == null) {
            throw new ServiceException(EnumRCode.DataNotExist.value, "该知识库下问题不存在");
        }
        boolean result = this.removeById(one);
        return new CmdResVo(1, one.getPid().toString());
    }

    @Override
    public IPage<KnowledgeQuestionVO> getKnowledgeQuestionList(KnowledgeQuestionQueryDTO query) {
        if (query == null) {
            throw new ServiceException(EnumRCode.ArgFormatErr.value, EnumRCode.ArgFormatErr.name);
        }
        return selectJoinListPage(new Page<>(query.getPageNum(), query.getPageSize()),
                KnowledgeQuestionVO.class,
                new MPJLambdaWrapper<WorkflowKnowledgeQuestion>()
                        .selectAll(WorkflowKnowledgeQuestion.class)
                        .eq(JPubUtils.IsNotEmpty(query.getKbId()), WorkflowKnowledgeQuestion::getKbId, query.getKbId())
                        .like(JPubUtils.IsNotEmpty(query.getTitle()), WorkflowKnowledgeQuestion::getTitle, query.getTitle())
                        .like(JPubUtils.IsNotEmpty(query.getTags()), WorkflowKnowledgeQuestion::getTags, query.getTags())
                        .eq(JPubUtils.IsNotEmpty(query.getStatus()), WorkflowKnowledgeQuestion::getStatus, query.getStatus())
                        .orderByDesc(WorkflowKnowledgeBase::getCreatedTime)
        );
//        IPage<WorkflowKnowledgeQuestion> page = new Page<>(query.getPageNum(), query.getPageSize());
//        IPage<WorkflowKnowledgeQuestion> result = this.page(page,
//                new LambdaQueryWrapper<WorkflowKnowledgeQuestion>()
//                        .eq(WorkflowKnowledgeQuestion::getKbId, query.getKbId())
//                        .like(WorkflowKnowledgeQuestion::getTitle, query.getTitle())
//                        .like(WorkflowKnowledgeQuestion::getTags, query.getTags())
//                        .eq(WorkflowKnowledgeQuestion::getStatus, query.getStatus())
//        );
//        return result.convert(question -> {
//            KnowledgeQuestionVO vo = new KnowledgeQuestionVO();
//            BeanUtils.copyProperties(question, vo);
//            return vo;
//        });
    }
}